//
//  UserService.m
//  Guazi
//
//  Created by koolearn on 11-5-30.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UserService.h"
#import "TwitterClient.h"
#import "OperationLog.h"
#import "DataUploadService.h"


@implementation UserService


+ (BOOL) addUser:(User*)user groups:(NSArray*)groups
{
    NSString* params= [NSString stringWithFormat:@"{userId:'%lld', name:'%@', remark:'%@', inContact:'%@', groups:[%@]}", user.userId, user.phone, user.remark, user.contact?@"true":@"false", [groups componentsJoinedByString:@","]];
    OperationLog* log = [[OperationLog alloc] initWithModule:kOperationLogModulePerson 
                                                   operation:kOperationLogOperationAdd 
                                                       refId:[NSString stringWithFormat:@"%lld",user.userId] params:[NSString stringWithFormat:params, user.userId]];
    [log insert];
    [log release];
    
    [user updateDB:YES withName:YES withAccount:YES];
    [FriendGroup deleteAllGroupForUser:user.userId];
    for ( NSString* groupId in groups ) {
        [FriendGroup insertGroupUser:user.userId groupId:[groupId longLongValue] seq:0];
    }
    
    [[DataUploadService sharedInstance] run];
    
    return YES;
}


+ (BOOL) createFriendship:(User*)user
{
    //BOOL result = [TwitterClient createFriendshipSync:user.userId];
    
    NSString* params= [NSString stringWithFormat:@"{userId:'%lld', name:'%@', remark:'%@', inContact:'%@'}", user.userId, user.phone, user.remark, user.contact?@"true":@"false"];
    OperationLog* log = [[OperationLog alloc] initWithModule:kOperationLogModulePerson 
                                                   operation:kOperationLogOperationAdd 
                                                       refId:[NSString stringWithFormat:@"%lld",user.userId] params:[NSString stringWithFormat:params, user.userId]];
    [log insert];
    [log release];

    user.following = YES;
    [user updateRelation];
    
    [[DataUploadService sharedInstance] run];
    return YES;
}


+ (BOOL) destroyFriendship:(User*)user
{
    //BOOL result = [TwitterClient destroyFriendshipSync:user.userId];
    
    OperationLog* log = [[OperationLog alloc] initWithModule:kOperationLogModulePerson 
                                                   operation:kOperationLogOperationDelete 
                                                       refId:[NSString stringWithFormat:@"%lld",user.userId] params:[NSString stringWithFormat:@"{userId:'%lld'}", user.userId]];
    [log insert];
    [log release];

    user.following = NO;
    [user updateRelation];
    
    [[DataUploadService sharedInstance] run];
    return YES;
}


+ (BOOL) setUserGroups:(long long)userId groups:(NSArray *)groups
{
    NSString* operation = @"reset";
    if ( [groups count] == 0 ) operation = @"remove";
 	
    //BOOL result = [TwitterClient setUserGroupsSync:groups userId:userId operation:operation];
    BOOL result = YES;
 
    if ( result ) {
        [FriendGroup deleteAllGroupForUser:userId];
        if ( [groups count] == 0 ) {
            //加入到未分组中
            [FriendGroup insertGroupUser:userId groupId:0 seq:0];
        }
        else {
            //增加到分组中
            for ( NSString* s in groups ) {
                [FriendGroup insertGroupUser:userId groupId:[s longLongValue] seq:0];
            }
        }
    }
    
    NSString* json = [NSString stringWithFormat:@"{operation:'%@', groups:'%@'}", operation, [groups componentsJoinedByString:@","]];
    OperationLog* log = [[OperationLog alloc] initWithModule:kOperationLogModulePerson 
                                                   operation:kOperationLogOperationSetPersonGroups 
                                                       refId:[NSString stringWithFormat:@"%lld", userId]
                                                      params:json];
    [log insert];
    [log release];
    
    [[DataUploadService sharedInstance] run];
    return result;
}




@end
